/**
 * @Author：ming
 * @Date：2023/9/30 4:04 PM
 * @Filename：Lc13
 */

import java.util.HashMap;

/**
 * @author ming
 */
public class Lc13 {
    public int romanToInt(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('I',1);
        map.put('V',5);
        map.put('X',10);
        map.put('L',50);
        map.put('C',100);
        map.put('D',500);
        map.put('M',1000);
        char[] cc = s.toCharArray();
        int sum =0;
        for (int i = cc.length-1; i >= 0; i--) {
            int current = map.get(cc[i]);
            if(i+1<cc.length){
                int pre = map.get(cc[i+1]);
                if(pre>current){
                    sum -= 2* current;
                }
            }
            sum += current;
        }

        return sum;
    }
}
